package com.example.transter;

import java.io.File;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class DomXml {

    public static void main(String[] args) {
        Long start = System.currentTimeMillis();
//        createXml();
        System.out.println("运行时间：" + (System.currentTimeMillis() - start));
    }

    /**
     * 生成xml方法
     */
    public static void createXml(List<String> list) {
        try {
// 创建解析器工厂
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = factory.newDocumentBuilder();
            Document document = db.newDocument();
// 不显示standalone="no"
            document.setXmlStandalone(true);
// 向bookstore根节点中添加子节点book
            Element urlset = document.createElement("urlset");
            urlset.setAttribute("content_method", "full");

            Element url = document.createElement("url");

            Element loc = document.createElement("loc");
            loc.setTextContent(list.get(0));
            Element lastmod = document.createElement("lastmod");
            Element changefreq = document.createElement("changefreq");
            Element priority = document.createElement("priority");
            priority.setTextContent("1.0");
            Element data = document.createElement("data");




            Element display = document.createElement("display");



            Element gameTypeName = document.createElement("gameTypeName");
            gameTypeName.setTextContent(list.get(1));
            Element gameScore = document.createElement("gameScore");
            gameScore.setTextContent(list.get(3));
            Element gameName = document.createElement("gameName");
            gameName.setTextContent(list.get(4));
            Element gameIntro = document.createElement("gameIntro");
            gameIntro.setTextContent(list.get(5));
            Element gameIcon = document.createElement("gameIcon");
            gameIcon.setTextContent(list.get(6));
            Element gameCoverUrl = document.createElement("gameCoverUrl");
            gameCoverUrl.setTextContent(list.get(7));
            Element gameCover = document.createElement("gameCover");
            gameCover.setTextContent(list.get(8));
            Element gameType = document.createElement("gameType");
            gameType.setTextContent(list.get(9));
            Element textChainUrl = document.createElement("textChainUrl");
            textChainUrl.setTextContent(list.get(10));
            Element textChainTitle1 = document.createElement("textChainTitle1");
            textChainTitle1.setTextContent(list.get(11));
            Element textChainUrl2 = document.createElement("textChainUrl2");
            textChainUrl2.setTextContent(list.get(12));
            Element textChainTitle2 = document.createElement("textChainTitle2");
            textChainTitle2.setTextContent(list.get(13));
            Element textChainUrl3 = document.createElement("textChainUrl3");
            textChainUrl3.setTextContent(list.get(14));
            Element textChainTitle3 = document.createElement("textChainTitle3");
            textChainTitle3.setTextContent(list.get(15));
            Element backGroundImage = document.createElement("backGroundImage");
            backGroundImage.setTextContent(list.get(16));
            Element backGroundImageUrl = document.createElement("backGroundImageUrl");
            backGroundImageUrl.setTextContent(list.get(17));
            Element bannerImage = document.createElement("bannerImage");
            bannerImage.setTextContent(list.get(18));
            Element bannerImageUrl = document.createElement("bannerImageUrl");
            bannerImageUrl.setTextContent(list.get(19));
            Element listUrl = document.createElement("listUrl");
            listUrl.setTextContent(list.get(20));
            Element releaseTime = document.createElement("releaseTime");
            releaseTime.setTextContent(list.get(22));
            Element playStation = document.createElement("playStation");
            playStation.setTextContent(list.get(21));


            display.appendChild(gameTypeName);
            display.appendChild(gameScore);
            display.appendChild(gameName);
            display.appendChild(gameIntro);
            display.appendChild(gameIcon);
            display.appendChild(gameCoverUrl);
            display.appendChild(gameCover);
            display.appendChild(gameType);
            display.appendChild(textChainUrl);
            display.appendChild(textChainTitle1);
            display.appendChild(textChainUrl2);
            display.appendChild(textChainTitle2);
            display.appendChild(textChainUrl3);
            display.appendChild(textChainTitle3);
            display.appendChild(backGroundImage);
            display.appendChild(backGroundImageUrl);
            display.appendChild(bannerImage);
            display.appendChild(bannerImageUrl);
            display.appendChild(listUrl);
            display.appendChild(releaseTime);
            display.appendChild(playStation);

            data.appendChild(display);

            url.appendChild(loc);
            url.appendChild(lastmod);
            url.appendChild(changefreq);
            url.appendChild(priority);
            url.appendChild(data);

            urlset.appendChild(url);

            document.appendChild(urlset);
// 创建TransformerFactory对象
            TransformerFactory tff = TransformerFactory.newInstance();
// 创建 Transformer对象
            Transformer tf = tff.newTransformer();

// 输出内容是否使用换行
            tf.setOutputProperty(OutputKeys.INDENT, "yes");
// 创建xml文件并写入内容
            tf.transform(new DOMSource(document), new StreamResult(new File(list.get(4)+ ".xml")));
            System.out.println("生成"+ list.get(4)  +".xml成功");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("生成book1.xml失败");
        }
    }
}


